import { App, Home, MenuItem } from "@/seed-application/types"
import { seedApplication } from ".."

export class MenuManager {
  private app: App

  public init(app: App) {
    this.app = app
  }

  public getMenus() {
    const ret: MenuItem[] = []
    if(!this.app.groups) return ret

    const groups = this.app.groups
    const group = this.app.groups['']
    const items = (group?.items || []).filter(e => this.app.tenant || e !== 'tenant')
    ret.push(...this.getMenuItems(items))

    for(const groupName in groups) {
      if(!groupName) continue
      
      const { name, label, icon, items } = groups[groupName]
      const children: MenuItem[] = this.getMenuItems(items)
      if(children.length > 0) ret.push({name, path: `/${name}`, label, icon, children})
    }

    return ret
  }

  private getMenuItems(items: string[]) {
    const ret: MenuItem[] = []
    if(!items || items.length === 0) return ret

    for(const item of items) {
      const microAndName = item.split('.')
      const len = microAndName.length
      const microName = len == 1 ? void 0 : microAndName[0]
      const whatName = len == 1 ? microAndName[0] : microAndName[1]
      const what = this.getWhat(whatName, microName)
      if(what) {
        const {label, icon, mode, component, componentProps} = what as Home
        const name = (microName || '') + whatName
        const path = (microName ? `/${microName}` : '') + `/${whatName}`
        ret.push({name, path, label, icon: icon || 'ep:grid', mode, component, componentProps})
      }
    }

    return ret
  }  

  public getWhat(name: string, microName?: string) {
    const home = seedApplication.getHome(name, microName)
    if(home) return home

    const action = seedApplication.getAction(name, void 0, microName)
    if(action) return action

    const seed = seedApplication.getSeed(name, microName)
    if(seed && seed.type != 'through') return seed

    return void 0
  }

//   public getMicroMenus(positions: SeedPosition[]) {
//     const ret: MenuItem[] = []
//     const userStore = useUserStore()
//     if(!userStore.isLogin) return ret

//     const actions = this.app.actions
//     if(actions) ret.push(...this.getActionMenus(actions, ...positions))

//     const homes = this.app.homes
//     if(homes) ret.push(...this.getHomeMenus(homes, ...positions))

//     const seeds = this.app.seeds
//     if(seeds) ret.push(...this.getSeedMenus(seeds, ...positions))

//     const micros = this.app.micros
//     if(micros) {
//       for(const microName in micros) {
//         const micro = micros[microName]
//         if(micro.actions) ret.push(...this.getMicroActionMenus(micro, micro.actions))
//         if(micro.homes) ret.push(...this.getMicroHomeMenus(micro, micro.homes))
//         if(micro.seeds) ret.push(...this.getMicroSeedMenus(micro, seeds))
//       }
//     }
// console.log('getMicroMenus===', ret)
//     return ret
//   }

  // private getActionMenus(actions: Record<string, Action>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const actionName in actions) {
  //     const action = actions[actionName]
  //     const { position, label, icon } = action
  //     if(!position) continue

  //     const [component, componentProps] = componentManager.getActionComponent(action)
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: actionName,
  //         label,
  //         path: `/${actionName}`,
  //         icon: icon || 'fluent-mdl2:set-action',
  //         mode: action.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }

  // private getHomeMenus(homes: Record<string, Home>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const homeName in homes) {
  //     const home = homes[homeName]
  //     const { position, label, icon } = home
  //     if(!position) continue

  //     const [component, componentProps] = this.componentManager.getHomeComponent(home)
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: homeName,
  //         label,
  //         path: `/${homeName}`,
  //         icon: icon || 'ic:outline-home',
  //         mode: home.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }

  // private getSeedMenus(seeds: Record<string, Seed>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const seedName in seeds) {
  //     const seed = seeds[seedName]
  //     const { position, label, icon } = seed
  //     if(!position) continue

  //     const [component, componentProps] = this.componentManager.getSeedComponent(seed)
  //     console.log('position=', position)
  //     console.log('positions=', positions)
  //     console.log('position.some(e => positions.includes(e))', position.some(e => positions.includes(e)))
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: seedName,
  //         label,
  //         path: `/${seedName}`,
  //         icon: icon || 'streamline-ultimate-color:module-hands-puzzle',
  //         mode: seed.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }

  // private getMicroActionMenus(micro: Micro, actions: Record<string, Action>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const actionName in actions) {
  //     const action = actions[actionName]
  //     const { position, label, icon } = action
  //     if(!position) continue

  //     const [component, componentProps] = this.componentManager.getMicroActionComponent(micro, action)
  //     const microName = micro.name
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: `${microName}/${actionName}`,
  //         label,
  //         path: `${microName}/${actionName}`,
  //         icon,
  //         mode: action.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }

  // private getMicroHomeMenus(micro: Micro, homes: Record<string, Home>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const homeName in homes) {
  //     const home = homes[homeName]
  //     const { position, label, icon } = home
  //     if(!position) continue

  //     const [component, componentProps] = this.componentManager.getMicroHomeComponent(micro, home)
  //     const microName = micro.name
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: `${microName}/${homeName}`,
  //         label,
  //         path: `${microName}/${homeName}`,
  //         icon,
  //         mode: home.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }

  // private getMicroSeedMenus(micro: Micro, seeds: Record<string, Seed>, ...positions: SeedPosition[]) {
  //   const ret: MenuItem[] = []
  //   for(const seedName in seeds) {
  //     const seed = seeds[seedName]
  //     const { position, label, icon } = seed
  //     if(!position) continue

  //     const [component, componentProps] = this.componentManager.getMicroSeedComponent(micro, seed)
  //     const microName = micro.name
  //     if(position.some(e => positions.includes(e))) {
  //       ret.push({
  //         name: `${microName}/${seedName}`,
  //         label,
  //         path: `${microName}/${seedName}`,
  //         icon,
  //         mode: seed.mode,
  //         component,
  //         componentProps
  //       })
  //     }
  //   }

  //   return ret
  // }
}